//
//  HYGoodsViewController.m
//  HaiYue
//
//  Created by skyline on 2018/6/15.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYGoodsViewController.h"
#import "HYGoodsCell.h"
#import "HYGoodsModel.h"
CGFloat const HYEXCHANGEHEITHG = 44;
@interface HYGoodsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) HYButton *button;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HYGoodsModel *model;
@property (nonatomic, assign) CGFloat cellH;

@end

@implementation HYGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品详情";
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = NO;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, HYEXCHANGEHEITHG + GAP * 2, 0));
        }];
    }
    return _tableView;
}

- (HYButton *)button
{
    if (!_button)
    {
        _button = [[HYButton alloc] initWithTitle:@"我要兑换" backColor:THEME_COLOR];
        [_button addTarget:self action:@selector(actionForExchange:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).with.offset(- GAP);
            make.left.equalTo(self.view).with.offset(GAP);
            make.right.equalTo(self.view).with.offset(-GAP);
            make.height.mas_offset(HYEXCHANGEHEITHG);
        }];
    }
    return _button;
}

- (void)actionForExchange:(HYButton *)sender
{
    if (![HYUserManager manager].user.accesstoken)
    {
        [CCAlert showMessage:@"检测到未登录，请先登录" hidden:^{
            [self.navigationController pushViewController:[NSClassFromString(@"HYLoginViewController") new] animated:YES];
        }];
    }
    else if ([[HYUserManager manager].user.viplevel intValue] < [self.model.viplevel intValue])
    {
        [CCAlert showMessage:@"兑换失败,VIP等级不够" hidden:nil];
    }
    else{
        [self startHUD];
        [CCHttp post:API_SHOP_CHANGE para:@{@"goodsid":self.goodsid} success:^(id  _Nonnull response) {
            [self endHUD];
            [CCAlert showMessage:[response valueForKey:@"msg"] hidden:^{
                
            }];
        } fail:^(NSError * _Nonnull error) {
            [self endHUD];
        }];
    }
}

- (void)https
{
    [self startHUD];
    [CCHttp post:API_SHOP_DETAIL para:@{@"goodsid":self.goodsid} success:^(id  _Nonnull response) {
        [self endHUD];
        int code = [[response valueForKey:@"retcode"] intValue];
        if (code == 100)
        {
            self.model = [HYGoodsModel mj_objectWithKeyValues:[response valueForKey:@"data"]];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
    }];
//    [HYHttps post:API_SHOP_DETAIL para:@{@"goodsid":self.goodsid} complete:^(id response) {
//        
//        [self endHUD];
//        int code = [[response valueForKey:@"retcode"] intValue];
//        if (code == 100)
//        {
//            self.model = [HYGoodsModel mj_objectWithKeyValues:[response valueForKey:@"data"]];
//            [self.tableView reloadData];
//        }
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString  stringWithFormat:@"HYGoodsCell%ld",indexPath.row];
    
    HYGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[HYGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.type = (indexPath.row == 0)?HYGoodsCellTypeImage:HYGoodsCellTypeText;
    cell.line.hidden = (indexPath.row == 3)?YES:NO;
    if (indexPath.row == 0)
    {
        cell.imgURL = _model.pic;
        cell.imgTitle = _model.goodsname;
        cell.jifen = _model.vipscore;
        cell.vip = _model.viplevel;
    }
    else if (indexPath.row == 1)
    {
        cell.title = @"商品介绍";
        cell.content = _model.goodsinfo;
    }
    else if (indexPath.row == 2)
    {
        cell.title = @"兑换流程";
        cell.content = _model.process;
    }
    else
    {
        cell.title = @"注意事项";
        cell.content = _model.notice;
    }
    self.cellH = cell.height;
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellH;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.style = HYViewNaviBarStyleOnlyLeftBtn;
    self.button.hidden = NO;
    [self https];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
